package com.example.wengwyue.patientapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wengwyue.patientapplication.R;
import com.hospital.common.api.secure.AESKey;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册验证手机
 * Created by lingquan(quan.ling@hotmail.com) on 16/8/13.
 */
public class RegisterVerifyPhoneFragment extends AbsVerifyPhoneFragment {
    @Bind(R.id.verify_phone_editText)
    EditText mVerifyPhoneEditText;
    @Bind(R.id.verify_code_editText)
    EditText mVerifyCodeEditText;
    @Bind(R.id.get_code_btn)
    TextView mGetCodeBtn;
    @Bind(R.id.resend_code_btn)
    TextView mResendCodeBtn;
    @Bind(R.id.msg_send_tip)
    TextView mMsgSendTip;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register_verify_phone, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.get_code_btn, R.id.verify_phone_textView})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.verify_phone_textView:
                verifyCode(2, mVerifyCodeEditText.getText());
                break;
            case R.id.get_code_btn:
                sendCode(2, mVerifyPhoneEditText.getText());
                break;
        }
    }

    @Override
    protected void onSendMsgSuccess(long phone, String code) {
        super.onSendMsgSuccess(phone, code);

        mMsgSendTip.setVisibility(View.VISIBLE);
        mMsgSendTip.setText(String.format("验证码短信已发送至%s", phone));

        mGetCodeBtn.setVisibility(View.GONE);
        mResendCodeBtn.setVisibility(View.VISIBLE);

        mVerifyCodeEditText.setText(code);
    }

    @Override
    protected void onTimeTick(int remain) {
        super.onTimeTick(remain);

        if (remain <= 0) {
            mGetCodeBtn.setVisibility(View.VISIBLE);
            mGetCodeBtn.setText("重新发送");
            mResendCodeBtn.setVisibility(View.GONE);
        } else {
            mResendCodeBtn.setText(String.format("%s秒后重发", remain));
        }
    }

    @Override
    protected void onVerifyCodeSuccess(long phone, AESKey aesKey) {
        super.onVerifyCodeSuccess(phone, aesKey);

        FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
        RegisterSetInfoFragment f = new RegisterSetInfoFragment();
        Bundle arg = new Bundle();
        arg.putString("PHONE", String.valueOf(phone));
        arg.putParcelable("AESKEY", aesKey);
        f.setArguments(arg);
        t.replace(R.id.fragment_container, f);
        t.commit();
    }
}
